package cn.itsource.pet.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.IdUtil;
import cn.itsource.basic.service.impl.BaseServiceImpl;
import cn.itsource.basic.util.AjaxResult;
import cn.itsource.basic.util.GetUserInfoUtil;
import cn.itsource.basic.util.PageList;
import cn.itsource.basic.util.RedisService;
import cn.itsource.basic.util.baidumap.DistanceUtil;
import cn.itsource.org.domain.Shop;
import cn.itsource.org.mapper.ShopMapper;
import cn.itsource.pet.domain.OrderPetAcquisition;
import cn.itsource.pet.domain.Pet;
import cn.itsource.pet.domain.SearchMasterMsg;
import cn.itsource.pet.mapper.OrderPetAcquisitionMapper;
import cn.itsource.pet.mapper.PetMapper;
import cn.itsource.pet.mapper.SearchMasterMsgMapper;
import cn.itsource.pet.query.SearchMasterMsgQuery;
import cn.itsource.pet.service.ISearchMasterMsgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
//Propagation.SUPPORTS:支持别人事务
@Transactional
public class SearchMasterMsgServiceImpl extends BaseServiceImpl<SearchMasterMsg, SearchMasterMsgQuery> implements ISearchMasterMsgService {

    @Autowired
    private RedisService redisService;

    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;
    @Autowired
    private OrderPetAcquisitionMapper orderPetAcquisitionMapper;
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private PetMapper petMapper;

    @Override
    public PageList<SearchMasterMsg> findPage(SearchMasterMsgQuery query) {

        //从redis里面来 GetUserINfoUtil
        Long shopId = GetUserInfoUtil.getShopId();
        query.setShopId(shopId);

        //0.通过shopId查询到店铺,获取到店铺的地址了
        Shop shop = shopMapper.selectByPrimaryKey(shopId);
        String shopAddress = shop.getAddress();

        //1.现在返回数据只是基础列表,所有店铺id为null或者自己店铺都出来,这个数据有可能在千里之外吧  有100条
        List<SearchMasterMsg> masterMsgs = searchMasterMsgMapper.findPage(query);

        if (CollectionUtil.isEmpty(masterMsgs)){
            return  new PageList<>();
        }

        //2.根据工具类判断是否距离店铺5km之内,才是有效数据 //30条  filter:过滤,   符合表达式条件就会收集起来toList集合  过滤条件:距离5km之内
        List<SearchMasterMsg> msgList = masterMsgs.stream().filter(msg -> DistanceUtil.isInCircle(shopAddress, msg.getAddress(), 5000))
                .collect(Collectors.toList());


        if (CollectionUtil.isEmpty(msgList)){
            return  new PageList<>();
        }

        //3.手动分页
        //skip:跳过  30条  20条  limit 10  取20条的前10条
        //查询模拟   limit 0,10  limit 10,10  limit (page - 1)* size ,size
        List<SearchMasterMsg> list = msgList.stream().skip((query.getCurrentPage() - 1) * query.getPageSize()).limit(query.getPageSize())
                .collect(Collectors.toList());

        //总条数  查询页的数据
        return new PageList<>((long) msgList.size(),list);
    }

    public static void main(String[] args) {

    }


    @Override
    public AjaxResult order(SearchMasterMsg msg) {

        //1.根据msg的id查到这个数据的对象
        Long id = msg.getId();
        SearchMasterMsg dbMsg = searchMasterMsgMapper.selectByPrimaryKey(id);
        //2.copy对象到订单对象
        OrderPetAcquisition orderPetAcquisition = BeanUtil.copyProperties(dbMsg, OrderPetAcquisition.class);
        //2.1设置
        orderPetAcquisition.setDigest(msg.getTitle());
        orderPetAcquisition.setEmpId(msg.getEmpId());
        orderPetAcquisition.setMsgId(msg.getId());
        orderPetAcquisition.setOrderSn(IdUtil.getSnowflake().nextId()+"");

        orderPetAcquisition.setId(null);

        Long shopId = GetUserInfoUtil.getShopId();
        orderPetAcquisition.setShopId(shopId);

        //update Msg
        dbMsg.setShopId(GetUserInfoUtil.getShopId());
        dbMsg.setState(1);
        searchMasterMsgMapper.updateByPrimaryKeySelective(dbMsg);

        //todo 健壮性判断,判断order的msg_id已经有了,就拦截

        //3.订单入库
        orderPetAcquisitionMapper.insertSelective(orderPetAcquisition);


        //4.入库pet表,默认未上架
        Pet pet = BeanUtil.copyProperties(dbMsg, Pet.class);
        pet.setId(null);
        pet.setTypeId(dbMsg.getPetType());
        pet.setState(0L);
        pet.setCreateTime(new Date());

        petMapper.insertSelective(pet);


        return AjaxResult.createSuccess();

    }


    //todo 插眼   后面的卧龙完成了,我离职了
}
